package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * @Author YuanYuan
 * @Date 2023/4/18
 * @Time 22:16
 */

@Controller // 当前类为控制器
@ResponseBody// 返回的是数据,而非页面
public class TestController {

    @Value("${mytest}")
    private String mytest;
    @Value("${mytest2}")
    private String mytest2;

    @Value("${myString}")
    private String myString;

    @Value("${myString1}")
    private String myString1;

    @Value("${myString2}")
    private String myString2;

    @Autowired
    private Student student;

    @PostConstruct
    public void postConstruct(){
        System.out.println("myString: "+myString);
        System.out.println("myString1: "+myString1);
        System.out.println("myString2: "+myString2);
        System.out.println(student);

    }

    @RequestMapping("/hi")// URL 路由注册,用于完成映射
    public String sayHi(String name) {
        //为空为null,默认值处理
        //如果name 没有长度 等同于 (name == null || name.equals(""))
        if(!StringUtils.hasLength(name)){
            name = "张三";
        }
        return "你好: " + name;
    }

    @RequestMapping("/getconf")
    public String getConf() {
        return "mytest: "+mytest+" "
                + "mytest2: "+ mytest2;
    }
}
